package com.ak.server.platform.service.impl;

import java.io.Serializable;

import org.springframework.stereotype.Service;

import com.ak.common.enums.Enums;
import com.ak.common.exception.AKException;
import com.ak.server.platform.entity.Dict;
import com.ak.server.platform.entity.DictItem;
import com.ak.server.platform.mapper.DictMapper;
import com.ak.server.platform.service.IDictItemService;
import com.ak.server.platform.service.IDictService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import lombok.RequiredArgsConstructor;

/**
 * @author Vean
 * @since 2019-05-17
 */
@Service
@RequiredArgsConstructor
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements IDictService {

    private final IDictItemService iDictItemService;

    @Override
    public boolean save(Dict entity) {
        return super.save(entity);
    }

    @Override
    public boolean updateDict(Dict sysDict) {
        return baseMapper.updateById(sysDict) > 0;
    }

    @Override
    public boolean removeById(Serializable id) {
        int count = iDictItemService.count(Wrappers.<DictItem>lambdaQuery().eq(DictItem::getDictId, id).eq(DictItem::getDelFlag, Enums.DelFlag.EXIST.getCode()));
        if (count > 0) {
            throw new AKException("该字典详细有数据，无法删除");
        }
        return super.update(Wrappers.<Dict>lambdaUpdate().set(Dict::getDelFlag, "1").eq(Dict::getId, id));
    }
}
